<?php

    /**
     * This class will handle survey creation and manipulation.
     */
    class SurveysController extends LSYii_Controller
    {
        public $layout = 'bare';
        public $defaultAction = 'index';

        public function actionPublicList($sLanguage = null)
        {
            if (isset($sLanguage))
            {
                App()->setLanguage($sLanguage);
            }

            $this->render('publicSurveyList', array(
                'publicSurveys' => Survey::model()->active()->open()->public()->with('languagesettings')->findAll(),
                'futureSurveys' => Survey::model()->active()->registration()->public()->with('languagesettings')->findAll(),

            ));
        }
        public  function actionIndex()
        {
            $this->layout = false;
            //$imgpath = Yii::app()->getconfig("publicstyleurl");
            $criteria=new CDbCriteria;
            $criteria->order='category ASC ,sid DESC';
            $criteria->limit= 25;
            $publicSurveys = Survey::model()->active()->open()->public()->with('languagesettings')->findAll($criteria);
            $surveysInfo = array();
            foreach($publicSurveys as $survey)
            {
                $sid = $survey->sid;
                //查询已参加答题人数
                $query = "SELECT count( id ) as nums FROM {{survey_".intval($sid)."}}";
                if(Yii::app()->getConfig("filterout_incomplete_answers") == true){
                    $query .= " WHERE  {{survey_".intval($sid)."}}.submitdate is not null";
                }
                $result = Yii::app()->db->createCommand($query)->queryAll();
                $number = 0;
                if($result){
                    foreach ($result as $res)
                    {
                        $number = $res['nums'];
                    }
                }else{
                    $number = "未知";
                }
                $category_id = (string)$survey->category;
                if($category_id&&isset($surveysInfo[$category_id])){
                    $surveysInfo[$category_id]['surveys'][] = array('sid'=>$sid,'title'=>$survey->localizedTitle,'number'=>$number);
                    
                }else{
                    $cid = (int)$category_id;
                    $sql = "SELECT * FROM {{survey_category}} WHERE id=$cid";
                    $result = Yii::app()->db->createCommand($sql)->queryAll();

                    foreach ($result as $res){
                        $surveysInfo[$category_id]['surveys'][]=array('sid'=>$sid,'title'=>$survey->localizedTitle,'number'=>$number);
                        $surveysInfo[$category_id]['cate']=$res;
                    }                
                }
               // $surveysInfo[] =array('sid'=>$sid,'title'=>$survey->localizedTitle,'number'=>$number,'category'=>$survey->category);
            }
            
  
            
            $this->render('index',array('info'=>$surveysInfo));
        }
        
    }
?>
